package com.aaa.sys.service.impl;

import com.aaa.sys.entity.Role;
import com.aaa.sys.mapper.RoleMapper;
import com.aaa.sys.service.RoleService;
import com.aaa.sys.vo.RoleSearch;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author ykq
 * @since 2020-11-25
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public List<String> queryRolesByUserId(Long userId) {
        return roleMapper.queryRolesByUserId(userId);
    }

    @Override
    public Page<Role> searchRole(Integer page, Integer limit, RoleSearch dept) {
        Page<Role> pageinfo=new Page<>(page,limit); //封装一个分页类
        String fullName = dept.getFullName();
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>(); //封装了一个查询条件类
        if(StringUtils.isNotEmpty(fullName)){
            queryWrapper.like("name",fullName);
        }
        this.baseMapper.selectPage(pageinfo,queryWrapper);
        return pageinfo;
    }

    @Override
    public void saveRolePermission(Long roleId, List<Long> menuIds) {

        this.roleMapper.deleteRolePermission(roleId);
        this.roleMapper.saveRolePermission(roleId,menuIds);
    }

    @Override
    public boolean removeRoleById(Long roleId) {
        this.roleMapper.deleteRolePermission(roleId);
        this.roleMapper.deleteById(roleId);
        return true;
    }

    @Override
    public List<Long> queryRoleIdsByUserId(Long userId) {
        return roleMapper.queryRoleIdsByUserId(userId);
    }
}
